package day9;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

import org.junit.Test;

public class IODemo {

	@Test
	public void	testFileReader() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("b.txt");
			// Returns:The character read, or -1 if the end of the stream has been reached
			int ch1 = fileReader.read();
			System.out.println(ch1);// 97
			System.out.println((char)ch1);// a
			
			int ch2 = fileReader.read();
			System.out.println(ch2);// 98
			System.out.println((char)ch2);// b
			
			int ch3 = fileReader.read();
			System.out.println(ch3);// 99
			System.out.println((char)ch3);// c
			
			int ch4 = fileReader.read();
			System.out.println(ch4);// -1
			System.out.println((char)ch4);//
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}
		}
	}
	
	@Test
	public void testFileReader2() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("a.txt");
			while (true) {
				int ch = fileReader.read();
				System.out.println((char)ch);
				if (ch == -1) {
					break;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}
		}
	}
	
	@Test
	public void testFileReader3() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("a.txt");
			int ch = -1;
			while ((ch = fileReader.read()) != -1) {
				System.out.println((char)ch);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}
		}
	}
	
	@Test
	public void testFileReader4() {
		FileReader fileReader = null;
		try {
			// int read(char[] cbuf)  
			// 返回值： 读取的字符数，如果已到达流的末尾，则返回 -1 
			fileReader = new FileReader("a.txt");
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println(length);
				System.out.println(Arrays.toString(buffer));
			}
			// 2048 + 30 = 2078
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testFileReader6() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			// int read(char[] cbuf)  
			// 返回值： 读取的字符数，如果已到达流的末尾，则返回 -1 
			fileReader = new FileReader("a.txt");
			fileWriter = new FileWriter("b.txt");
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println(length);
				System.out.println(Arrays.toString(buffer));
				// 读取多少就写多少，最后一次读取的数据可能不够buffer数组长度的数据
				fileWriter.write(buffer, 0, length);;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFileInputStream() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("a.jpg");
			fileOutputStream = new FileOutputStream("b.jpg");
			byte[] buffer = new byte[1024];
			int length = -1;
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testfileReader2() {
		FileReader fileReader=null;
		try {
			fileReader=new FileReader("a.txt");
			while (true) {
				int ch=fileReader.read();
				System.out.println((char)ch);
				if(ch==-1) {
					break;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			if (fileReader!=null) {
				try {
					fileReader.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}
		System.out.println("--------------------------");
	}
	
	@Test
	public void test55() {
		// 没有实现序列化
		// java.io.NotSerializableException: day9.Student
		Student student = new Student(1, "张三", 23, "男");
		FileOutputStream fileOutputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("student.data");
			objectOutputStream = new ObjectOutputStream(fileOutputStream);
			objectOutputStream.writeObject(student);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void test66() {
		FileInputStream fileInputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			fileInputStream = new FileInputStream("student.data");
			objectInputStream = new ObjectInputStream(fileInputStream);
			Student student = (Student) objectInputStream.readObject();
			System.out.println(student);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
}
